from PyQt5.QtCore import QObject
from docx import Document
from docx.shared import Cm
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.enum.table import WD_ALIGN_VERTICAL, WD_ROW_HEIGHT_RULE


class CWord(QObject):
    def __init__(self):
        super().__init__()
        self.document = Document()

    def new_docx(self, tile, full_path, data):
        col_num = len(data)
        row_num = len(data[0])
        p = self.document.add_paragraph(tile)
        p.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
        self.document.add_paragraph(self.tr('Name') + ':__________               ' +
                                    self.tr('Elapsed Time') + ':__________               ' +
                                    self.tr('Score') + ':__________')
        table = self.document.add_table(rows=row_num, cols=col_num, style='Table Grid')
        for col in range(col_num):
            for row in range(row_num):
                table.cell(row, col).vertical_alignment = WD_ALIGN_VERTICAL.TOP | WD_ALIGN_VERTICAL.BOTTOM
                table.rows[row].height = Cm(0.79)
                cell = table.cell(row, col)
                first = data[col][row]['first_num']
                second = data[col][row]['second_num']
                label = data[col][row]['operate_label_val']
                cell.text = '{0} {1} {2}=____'.format(first, label, second)

        self.document.save(full_path + '.docx')